//
//  selectRegionView.m
//  anFang
//
//  Created by tangtangchitangtang on 2017/9/11.
//  Copyright © 2017年 tangtangchitangtang. All rights reserved.
//

#import "selectRegionView.h"

@implementation selectRegionView
-(void)creatView{
    self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    _balckArr=[NSMutableArray arrayWithCapacity:0];
    
    _tableArr2=[NSMutableArray arrayWithCapacity:0];
    _index=1;
    UIView *view=[UIView new];
    [self addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.width.mas_equalTo(_mainW*0.7);
        make.height.mas_equalTo(_mainH*0.6);
    }];
    view.backgroundColor=[UIColor whiteColor];
    view.layer.cornerRadius=4;
    
    UIView *topView=[UIView new];
    [view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(55);
    }];
    topView.backgroundColor=_backgroundColor;
    
    UIButton *iamge=[UIButton new];
    [topView addSubview:iamge];
    [iamge mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(50);
    }];
    [iamge setImage:[UIImage imageNamed:@"返回1"] forState:0];
    [iamge addTarget:self action:@selector(tuiChuView:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLabel=[UILabel new];
    [topView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
    titleLabel.text=@"案件类型";
    titleLabel.font=[UIFont systemFontOfSize:18];
    titleLabel.textColor=[UIColor whiteColor];
    
    _tableView=[BaseTableView new];
    [view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(topView.mas_bottom);
    }];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"0",@"id", nil];
    [_balckArr addObject:dic];
    [ZQTools AFNPostDataUrl:@"api/index/area_list" Dict:dic andTableView:nil andViewController:self.viewController andSuccessBlock:^(id responseObject) {
        _tableArr=responseObject;
        [_tableView reloadData];
    } anderrorBlock:nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _tableArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *Dic=_tableArr[indexPath.row];
    static NSString *indentify = @"ReportCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentify];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentify];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.textAlignment = NSTextAlignmentCenter;//文字居中
    cell.textLabel.text = [Dic objectForKey:@"name"];
    cell.textLabel.textColor=ColorWithRGB(49, 49, 49);
    cell.textLabel.font=[UIFont systemFontOfSize:20];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView reloadData];
    
  
    NSDictionary *typeDic=_tableArr[indexPath.row];
    [_tableArr2 addObject:typeDic];
    [_balckArr addObject:typeDic];
    if (_index==_index2) {
        _customSelectedBlock(_tableArr2);
        [self removeFromSuperview];
        return;
    }
    NSNumber *ids=[typeDic objectForKey:@"id"];
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%@",ids],@"id", nil];
    [ZQTools AFNPostDataUrl:@"api/index/area_list" Dict:dic andTableView:nil andViewController:self.viewController andSuccessBlock:^(id responseObject) {
        _tableArr=responseObject;
        _index+=1;
        [tableView reloadData];
    } anderrorBlock:nil];
}

-(void)tuiChuView:(UIButton *)btn{
    if (_index==1) {
        [self removeFromSuperview];
    }
    else{
        NSDictionary *typeDic=_balckArr[_index-2];
        
        NSNumber *ids=[typeDic objectForKey:@"id"];
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%@",ids],@"id", nil];
        [ZQTools AFNPostDataUrl:@"api/index/area_list" Dict:dic andTableView:nil andViewController:self.viewController andSuccessBlock:^(id responseObject) {
            _tableArr=responseObject;
            _index-=1;
            [_tableView reloadData];
        } anderrorBlock:nil];
        
    }
    
}

#pragma mark————————————————————点击屏幕关闭键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    //    make.width.mas_equalTo(_mainW*0.7);
    //    make.height.mas_equalTo(_mainH*0.6);
    
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self ];
    
    if (!(touchPoint.y>_mainH/2-_mainH*0.3&&touchPoint.y<_mainH/2+_mainH*0.3&&touchPoint.x>_mainW*0.15&&touchPoint.x<_mainW*0.85)) {
        [self removeFromSuperview];
    }
}




@end
